package com.example.demo.controller;

import com.example.demo.constants.Constants;
import com.example.demo.model.*;
import com.example.demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 添加图书
     */
    @RequestMapping(value = "/insert")
    public Result<String> insertBook(Book book){
        // 1. 校验参数
        log.info("添加图书，接收到参数 book: {}", book);
        if(!StringUtils.hasLength(book.getBookName())
                || !StringUtils.hasLength(book.getAuthor())
                || book.getCount() == null
                || book.getPrice() == null
                || !StringUtils.hasLength(book.getPublish())
                || book.getStatus() == null){
            log.error("book 参数错误");
            return Result.fail("book 参数错误");
        }
        // 2. 添加图书
        try{
            Integer result = bookService.insertBook(book);
            if(result > 0){
                return Result.success("");
            }else{
                return Result.fail("添加失败");
            }
        }catch (Exception e){
            log.error("添加图书失败 e: ", e);
            return Result.fail("内部错误，请联系管理员");
        }
    }

    /**
     * 根据页码获取图书列表
     */
    @RequestMapping("/selectBookByPage")
    public Result<PageResult<Book>> selectBookByPage(PageRequest pageRequest, HttpSession session){
        // 参数校验
        log.info("查询图书列表，pageRequest: {}", pageRequest);
        if(pageRequest == null || pageRequest.getPageNum() == null
                || pageRequest.getLimit() == null){
            log.error("参数错误");
            return Result.fail("参数错误");
        }
        try{
            PageResult<Book> pageResult = bookService.selectBookByPage(pageRequest);
            return Result.success(pageResult);
        }catch(Exception e) {
            log.error("按页获取图书列表失败 e: ", e);
            return Result.fail("内部错误，请联系管理员");
        }
    }

    /**
     *根据 id 获取图书信息
     */
    @RequestMapping("/selectBookById")
    public Result<Book> selectBookById(Integer bookId){
        // 参数校验
        log.info("根据 id 查询图书信息，id: {}", bookId);
        if(bookId == null){
            log.error("bookId 错误");
            return Result.fail("参数错误");
        }
        try{
            Book book = bookService.selectBookById(bookId);
            return Result.success(book);
        }catch (Exception e) {
            log.error("根据 id 获取图书失败 e: ", e);
            return Result.fail("内部错误，请联系管理员");
        }
    }

    /**
     * 更新图书信息
     */
    @RequestMapping("/updateBook")
    public Result<Integer> updateBookById(Book book){
        log.info("更新图书信息, book:{}", book);
        try{
          Integer result = bookService.updateBookById(book);
          if(result > 0){
              return Result.success(result);
          }
          return Result.fail("更新失败，请检查输入的图书信息");
        }catch (Exception e) {
            log.error("更新图书信息失败 e: ", e);
            return Result.fail("内部错误，请联系管理员");
        }
    }

    /**
     * 删除图书
     */
    @RequestMapping("/deleteBookById")
    public Result<String> deleteBookById(Integer id){
        log.info("删除图书，id: {}", id);
        try{
            Integer result = bookService.deleteBookById(id);
            if(result >= 1){
                return Result.success("删除成功");
            }else{
                return Result.fail("删除失败，请联系管理员");
            }
        }catch(Exception e) {
            log.error("删除图书失败 e: ", e);
            return Result.fail("内部错误，请联系管理员");
        }
    }

    /**
     * 批量删除图书
     */
    @RequestMapping("/batchDeleteByIds")
    public Result<String> batchDeleteByIds(@RequestParam List<Integer> ids){
        log.info("批量删除图书，ids: {}", ids);
        if(ids == null){
            log.error("参数不合法");
            return Result.fail("参数不合法");
        }
        try{
            Integer result = bookService.batchDeleteByIds(ids);
            if(result > 0){
                log.error("批量删除成功");
                return Result.success("批量删除成功");
            }
            return Result.fail("删除错误，请联系管理员");
        } catch(Exception e) {
            log.error("删除图书异常 e: ", e);
            return Result.fail("内部错误，请联系管理员");
        }
    }
}
